package com.sheepone.util.algorithm;

import com.sheepone.util.enums.SortType;

import java.util.*;

/**
 * @author Miss.杨
 * @description
 * @since 2024/3/2 - 23:04
 */
public class SortUtil {
    public static final Comparator<String> STRING_DESC_COMPARATOR = Collections.reverseOrder();

    public static <T> void customized(Comparator<T> comparator, List<T> list) {
        list.sort(comparator);
    }

    public static <T> void customized(Comparator<T> comparator, T[] array) {
        Arrays.sort(array, comparator);
    }

    public static void tailNumSort(String prefix, List<String> list) {
        tailNumSort(prefix, list, SortType.TAIL_NUM_ASC);
    }

    public static void tailNumSort(String prefix, List<String> list, SortType sortType) {
        if (sortType != SortType.TAIL_NUM_ASC && sortType != SortType.TAIL_NUM_DESC) {
            return;
        }
        int prefixLen = prefix.length();
        int maxLen = list.stream().mapToInt(s -> s.length() - prefixLen).max().getAsInt();
        TreeMap<String, String> treeMap = new TreeMap<>(sortType == SortType.TAIL_NUM_ASC ? null : STRING_DESC_COMPARATOR);
        list.forEach(s -> {
            String key = "0".repeat(maxLen + prefixLen - s.length()) + s.substring(prefixLen);
            treeMap.put(key, s);
        });
        int i = 0;
        for (String value : treeMap.values()) {
            list.set(i++, value);
        }
    }
}
